home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / HEXDUMP.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  2KB  |  89 lines

  1. /*
  2. **  HEXDUMP.C - Dump a file.
  3. **
  4. **  This Program Written By Paul Edwards w/ modifications by Bob Stout
  5. **  Released to the public domain
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. static void dodump(FILE *fp, long start, long count);
  14. static void skipb(FILE *fp, long start);
  15.  
  16. main(int argc, char **argv)
  17. {
  18.       FILE *fp;
  19.       long start, count;
  20.  
  21.       if (argc < 2)
  22.       {
  23.             puts("Usage: HEXDUMP file_name [start] [length]");
  24.             return (EXIT_FAILURE);
  25.       }
  26.       if (argc > 2)
  27.             start = atol(*(argv + 2));
  28.       else  start = 0L;
  29.       if (argc > 3)
  30.             count = atol(*(argv + 3));
  31.       else  count = -1L;
  32.       fp = fopen(*(argv + 1), "rb");
  33.       if (fp == NULL)
  34.       {
  35.             printf("unable to open file %s for input\n", *(argv+1));
  36.             return (EXIT_FAILURE);
  37.       }
  38.       skipb(fp, start);
  39.       dodump(fp, start, count);
  40.       return (EXIT_SUCCESS);
  41. }
  42.  
  43. static void dodump(FILE *fp, long start, long count)
  44. {
  45.       int c, pos1, pos2;
  46.       long x = 0L;
  47.       char prtln[100];
  48.  
  49.       while (((c = fgetc(fp)) != EOF) && (x != count))
  50.       {
  51.             if (x%16 == 0)
  52.             {
  53.                   memset(prtln,' ',sizeof prtln);
  54.                   sprintf(prtln,"%0.6X   ", start + x);
  55.                   pos1 = 8;
  56.                   pos2 = 45;
  57.             }
  58.             sprintf(prtln + pos1, "%0.2X", c);
  59.             if (isprint(c))
  60.                   sprintf(prtln + pos2, "%c", c);
  61.             else  sprintf(prtln + pos2, ".");
  62.             pos1 += 2;
  63.             *(prtln+pos1) = ' ';
  64.             pos2++;
  65.             if (x % 4 == 3)
  66.                   *(prtln + pos1++) = ' ';
  67.             if (x % 16 == 15)
  68.                   printf("%s\n", prtln);
  69.             x++;
  70.       }
  71.       if (x % 16 != 15)
  72.             printf("%s\n", prtln);
  73.       return;
  74. }
  75.  
  76. static void skipb(FILE *fp, long start)
  77. {
  78.       long x = 0;
  79.  
  80.       if (start == 0)
  81.             return;
  82.       while (x < start)
  83.       {
  84.             fgetc(fp);
  85.             x++;
  86.       }
  87.       return;
  88. }
  89.